﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace UbbTimetable2
{

    /// <summary>
    /// A struct to store subgroup data.
    /// </summary>
    public struct Subgroup
    {
        int _sgId;
        String _groupName;
        String _sgName;

        public int SgId
        {
            get { return _sgId; }
            set { _sgId = value; }
        }

        public String SgName
        {
            get { return _sgName; }
            set { _sgName = value; }
        }

        public String GroupName
        {
            get { return _groupName; }
            set { _groupName = value; }
        }
    }

    /// <summary>
    /// A DAO that handles all data related to subgroups.
    /// </summary>
    public class SubgroupDao : Dao
    {
        private String err = "";

        /// <summary>
        /// The last error message.
        /// </summary>
        public String Err
        {
            get { return err; }
        }

        /// <summary>
        /// Returns the list of subgroups of a group/
        /// </summary>
        /// <param name="group">The group of the subgroups or "any" for all.</param>
        /// <returns>The list of subgroups.</returns>
        public List<Subgroup> GetSubgroupList(String group)
        {
            String query = "SELECT * FROM Alcsoport ";
            if (group != "any")
            {
                query += "WHERE Csoport = '" + group + "'";
            }

            SqlDataReader rdr = ExecuteReader(query, ref err);

            List<Subgroup> list = new List<Subgroup>();
            if (err == "OK")
            {
                while (rdr.Read())
                {
                    Subgroup item = new Subgroup();
                    item.SgId = Convert.ToInt32(rdr[0]);
                    item.GroupName = rdr[1].ToString().TrimEnd();
                    item.SgName = rdr[2].ToString().TrimEnd();

                    list.Add(item);
                }
            }
            CloseDataReader(rdr);
            return list;
        }

        /// <summary>
        /// Inserts a new subgroup.
        /// </summary>
        /// <param name="group">The group.</param>
        /// <param name="name">The subgroup name.</param>
        public void InsertSubgroup(int group, String name)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("INSERT INTO Alcsoport(Csoport, Nev) VALUES (");
            sb.Append(group);
            sb.Append(",'");
            sb.Append(name);
            sb.Append("')");

            err = ExecuteNonQuery(sb.ToString());
        }

        /// <summary>
        /// Updates a subgroup.
        /// </summary>
        /// <param name="id">The id of the subgroup.</param>
        /// <param name="group">The group.</param>
        /// <param name="name">The subgroup name.</param>
        public void UpdateSubgroup(int id, int group, String name)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("UPDATE Alcsoport SET Csoport = ");
            sb.Append(group);
            sb.Append(", Nev = '");
            sb.Append(name);
            sb.Append("' WHERE ID = ");
            sb.Append(id);

            err = ExecuteNonQuery(sb.ToString());
        }

        /// <summary>
        /// Deletes a subgroup.
        /// </summary>
        /// <param name="id">The id of the subgroup.</param>
        public void DeleteSubgroup(int id)
        {
            String command = "DELETE FROM Alcsoport WHERE ID = " + id.ToString();

            err = ExecuteNonQuery(command);
        }
    }
}
